-
Notifications
You must be signed in to change notification settings - Fork 0
/
Caesar Cipher.c
51 lines (49 loc) · 1.11 KB
/
Caesar Cipher.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @Repository HackerRank Soulutions
* @file Caesar Cipher
* @author Abdelrahman Ahmed Moussa (abdelrahman.ahmed0599@gmail.com)
* @copyright Copyright (c) 2024
*
*/
char* caesarCipher(char* s, int k)
{
static char qString[101]={0};
int i=0;
/*while ( k>=('z'-'a'+1) )
{
k-=('z'-'a'+1);
}*/
k=k%('z'-'a'+1); //26 alphapet
while (s[i])
{
if ((s[i]>='A')&&(s[i]<='Z')) //UpperCase
{
if ( (s[i]+k)>'Z' )
{
qString[i]= (((s[i]+k)%'Z')-1)+'A'; //qString[i]= ((('Z'-(s[i]+k))-1)+'A';
}
else
{
qString[i]=s[i]+k;
}
}
else if ((s[i]>='a')&&(s[i]<='z')) //UpperCase
{
if ( (s[i]+k)>'z' )
{
qString[i]= (((s[i]+k)%'z')-1)+'a'; //qString[i]= ((('z'-(s[i]+k))-1)+'a';
}
else
{
qString[i]=s[i]+k;
}
}
else
{
qString[i]=s[i];
}
i++;
}
qString[i]='\0';
return qString;
}